package com.cspg.design.ui.schemeActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cspg.design.CSPGApplication;
import com.cspg.design.R;
import com.cspg.design.bean.shemeBean.MyHighVoltageSchemeBean;
import com.cspg.design.bean.shemeBean.MyLowVoltageSchemeBean;
import com.cspg.design.dao.gen.DaoSession;
import com.cspg.design.presenter.SchemePresenter;
import com.cspg.design.utils.ActionBarUtil;
import com.cspg.design.utils.StatusBarUtil;
import com.cspg.design.widget.AlertListDialog;

import java.util.List;

import lombok.Data;
import rx.android.schedulers.AndroidSchedulers;

public class MySchemeActivity extends AppCompatActivity {

    private ListView lvHighVoltage;
    private ListView lvLowVoltage;
    public  static List<MyHighVoltageSchemeBean> myHighVoltageSchemeBeanList;
    public  static List<MyLowVoltageSchemeBean> myLowVoltageSchemeBeanList;
    private TextView tvNoData;
    private SchemePresenter schemePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_scheme);
        ActionBarUtil.setActionBar(this, R.string.my_scheme);
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);

        lvHighVoltage = findViewById(R.id.lv_my_scheme_high_voltage);
        lvLowVoltage = findViewById(R.id.lv_my_scheme_low_voltage);
        TextView hightVoltageTextView = new TextView(this);
        hightVoltageTextView.setText("架空方案");
        hightVoltageTextView.setPadding(2, 30, 2, 10);
        hightVoltageTextView.setTextColor(Color.BLACK);
        hightVoltageTextView.setTextSize(20);
        lvHighVoltage.addHeaderView(hightVoltageTextView, null, false);
        TextView lowVoltageTextView = new TextView(this);
        lowVoltageTextView.setText("低压方案");
        lowVoltageTextView.setTextColor(Color.BLACK);
        lowVoltageTextView.setTextSize(20);
        lowVoltageTextView.setPadding(2, 0, 2, 10);
        lvLowVoltage.addHeaderView(lowVoltageTextView, null, false);

        lvHighVoltage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                new AlertListDialog.ListBuilder(MySchemeActivity.this).
                        setDividerColorRes(R.color.color_divider)
                        .setDividerHeightRes(R.dimen.dp1)
                        .setTitle("操作选择")
                        .setIcon(R.drawable.ic_info)
                        .setCancelable(false)
                        .setItems(new String[]{"修改", "查看", "取消"}, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case 0:
                                        Intent intent = new Intent(MySchemeActivity.this, MyHighVoltageSchemeEditActivity.class);
                                        intent.putExtra("code", "0");
                                        MyHighVoltageSchemeBean myHighVoltageSchemeBean = myHighVoltageSchemeBeanList.get(position - 1);
                                        intent.putExtra("bean", myHighVoltageSchemeBean);
                                        startActivity(intent);
                                        break;
                                    case 1:
                                        Intent intent1 = new Intent(MySchemeActivity.this, MyHighVoltageSchemeEditActivity.class);
                                        intent1.putExtra("code", "1");
                                        MyHighVoltageSchemeBean myHighVoltageSchemeBean1 = myHighVoltageSchemeBeanList.get(position - 1);
                                        intent1.putExtra("bean", myHighVoltageSchemeBean1);
                                        startActivity(intent1);
                                        break;
                                    case 2:
                                        dialog.dismiss();
                                        break;
                                }
                            }
                        })
                        .create().show();

            }
        });
        lvLowVoltage.setOnItemClickListener((parent, view, position, id) -> new AlertListDialog.ListBuilder(MySchemeActivity.this).
                setDividerColorRes(R.color.color_divider)
                .setDividerHeightRes(R.dimen.dp1)
                .setTitle("操作选择")
                .setIcon(R.drawable.ic_info)
                .setCancelable(false)
                .setItems(new String[]{"修改", "查看", "取消"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                Intent intent = new Intent(MySchemeActivity.this, MyLowVoltageSchemeEditActivity.class);
                                intent.putExtra("code", "0");
                                MyLowVoltageSchemeBean myLowVoltageSchemeBean = myLowVoltageSchemeBeanList.get(position - 1);
                                intent.putExtra("bean", myLowVoltageSchemeBean);
                                startActivity(intent);
                                break;
                            case 1:
                                Intent intent1 = new Intent(MySchemeActivity.this, MyLowVoltageSchemeEditActivity.class);
                                intent1.putExtra("code", "1");
                                MyLowVoltageSchemeBean myLowVoltageSchemeBean1 = myLowVoltageSchemeBeanList.get(position - 1);
                                intent1.putExtra("bean", myLowVoltageSchemeBean1);
                                startActivity(intent1);
                                break;
                            case 2:
                                dialog.dismiss();
                                break;
                        }
                    }
                })
                .create().show());
        tvNoData = findViewById(R.id.tv_my_scheme_no_data);
        this.schemePresenter = new SchemePresenter(this,
                this.lvHighVoltage,
                this.lvLowVoltage);
        initData();
    }

    private void initData() {

        schemePresenter.getHighVoltageScheme();
        schemePresenter.getLowVoltageScheme();

//        DaoSession daoSession = ((CSPGApplication) getApplication()).getDaoSession();
//        List<MyHighVoltageSchemeBean> myHighVoltageSchemeBeans = daoSession.getMyHighVoltageSchemeBeanDao().queryBuilder().list();
//
//        this.myHighVoltageSchemeBeanList = myHighVoltageSchemeBeans;
//        String[] schemeNames = new String[myHighVoltageSchemeBeans.size()];
//        for (int i = 0; i < myHighVoltageSchemeBeans.size(); i++) {
//            schemeNames[i] = myHighVoltageSchemeBeans.get(i).getSchemeName();
//        }
//        if (schemeNames.length > 0) {
//            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MySchemeActivity.this, android.R.layout.simple_list_item_1, schemeNames);
//            lvHighVoltage.setAdapter(arrayAdapter);
//        }
//        List<MyLowVoltageSchemeBean> myLowVoltageSchemeBeans = daoSession.getMyLowVoltageSchemeBeanDao().queryBuilder().list();
//        this.myLowVoltageSchemeBeanList = myLowVoltageSchemeBeans;
//        String[] lowSchemeNames = new String[myLowVoltageSchemeBeans.size()];
//        for (int i = 0; i < myLowVoltageSchemeBeans.size(); i++) {
//            lowSchemeNames[i] = myLowVoltageSchemeBeans.get(i).getSchemeName();
//        }
//        if (lowSchemeNames.length > 0) {
//            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MySchemeActivity.this, android.R.layout.simple_list_item_1, lowSchemeNames);
//            lvLowVoltage.setAdapter(arrayAdapter);
//        }
//        if (myHighVoltageSchemeBeans.size() ==0 && myLowVoltageSchemeBeans.size() == 0)
//        {
//            tvNoData.setVisibility(View.VISIBLE);
//        }
    }
}